import {
  request
} from "../utils/util";
const app = getApp()
Component({
  data: {
    selected: 0,
    color: "#333",
    selectedColor: "#73A1FF",
    list: [{
      pagePath: "../index/index",
      iconPath: "../public/img/tabbar_index.png",
      selectedIconPath: "../public/img/tabbar_index_active.png",
      text: "首页"
    },{
      pagePath: "../user/index",
      iconPath: "../public/img/tabbar_user.png",
      selectedIconPath: "../public/img/tabbar_user_active.png",
      text: "我的"
    }]
  },
  attached() {},
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      console.log(data.index, data.path)
      const url = data.path
      wx.switchTab({
        url
      })
      this.setData({
        selected: data.index
      })
      if (!app.globalData.requestSubscribeMessage && data.index == 1) {
        request('/api/index/config', app.globalData.token, 'post').then(res => {
          console.log(res, '/api/index/config')
          if (res.code === 200) {
            const tmplIds = res.data
            console.log(app.globalData.is_new === 2, 'is_new')
            console.log(app.globalData.mainSwitch !== false, 'is_new2')
            if (app.globalData.is_new === 2 && app.globalData.mainSwitch !== false) {
              wx.requestSubscribeMessage({
                tmplIds: tmplIds || [],
                success(res) {
                  let requestSubscribeMessage = true
                  Object.keys(res).map(key => {
                    if (res[key] === 'reject') requestSubscribeMessage = false
                  })
                  app.globalData.requestSubscribeMessage = requestSubscribeMessage
                  console.log(res, 111);
                },
                fail(err) {
                  console.log(err);
                  // wx.showToast({
                  //   title: "订阅失败",
                  // })
                }
              })
            } else if (app.globalData.is_new === 2) {
                  wx.openSetting({
                    withSubscriptions: true,
                    success:res=> {
                      console.log(res, 'withSubscriptions')

                        wx.requestSubscribeMessage({
                          tmplIds: tmplIds || [],
                          success(res) {
                            let requestSubscribeMessage = true
                            Object.keys(res).map(key => {
                              if (res[key] === 'reject') requestSubscribeMessage = false
                            })
                            app.globalData.requestSubscribeMessage = requestSubscribeMessage
                            console.log(res, 111);
                          },
                          fail(err) {
                            console.log(err);
                            // wx.showToast({
                            //   title: "订阅失败",
                            // })
                          }
                        })
                    }
                  })
                
            }
          }
        })

      }
    },
  }
})